package com.zlz.mapreduce.writeable;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * @author: zlz
 * @date: 2022/1/1 21:44
 * @description:
 * @since: JDK1.8
 */
public class FlowReducer extends Reducer<Text, FlowBean, Text, FlowBean> {
    private final FlowBean outValue = new FlowBean();
    @Override
    protected void reduce(Text key, Iterable<FlowBean> values, Context context) throws IOException, InterruptedException {
        // 遍历集合累加值
        Long sumUp = 0L;
        Long sumDown = 0L;
        int count = 0;
        for (FlowBean value : values) {
            sumUp += value.getUpFlow();
            sumDown += value.getDownFlow();
            count++;
        }
        outValue.setCount(count);
        outValue.setUpFlow(sumUp);
        outValue.setDownFlow(sumDown);
        outValue.setTotalFlow();

        context.write(key, outValue);

    }
}
